package com.tg.app.helper;

import android.content.Context;

import com.tange.base.toolkit.AppUtil;
import com.tange.base.toolkit.PreferenceUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGApplicationBase;

import java.util.ArrayList;
import java.util.List;

public class ResolutionHelper {
    private static final String PLAYER_RESOLUTION_PRE_ = "player_single_resolution_";
    private String uuid;
    private int curIndex = 0;
    private List<Integer> list = new ArrayList<>();
    public ResolutionHelper(Context context, String uuid){
        this.uuid = uuid;
       init(context, uuid);
    }

    public int getCurIndex() {
        return curIndex;
    }

    public void setCurIndex(int curIndex) {
        this.curIndex = curIndex;
    }

    public void init(Context context, String uuid){
        String str  = PreferenceUtil.getString(context, PLAYER_RESOLUTION_PRE_ + uuid);
        list.clear();
        if (!StringUtils.isEmpty(str)){
            String[] arr = str.split(",");
            curIndex = Integer.parseInt(arr[arr.length - 1]);
            if (arr.length % 2 == 1 && curIndex < arr.length /2){
                for (int i = 0; i < arr.length - 1; i++){
                    list.add(Integer.parseInt(arr[i]));
                }
            }else{
                curIndex = 0;
            }
        }
    }

    public void saveResolution(){
        if (StringUtils.equals(uuid, this.uuid) && list.size() % 2 == 0 && curIndex < list.size() / 2){
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.size(); i++){
                sb.append(list.get(i));
                sb.append(",");
            }
            sb.append(curIndex);
            PreferenceUtil.setString(TGApplicationBase.getApplication(), PLAYER_RESOLUTION_PRE_ + uuid, sb.toString());
        }
    }

    public void addAndSwitchResolution(int width, int height){
        if (width <= 0 || height <= 0){
            return;
        }
        for (int i = 1; i < list.size(); i += 2){
            if (list.get(i) == height && list.get(i - 1) == width){
                curIndex = i / 2;
                return;
            }
        }

        list.add(width);
        list.add(height);
        curIndex = list.size() / 2 - 1;
        saveResolution();
    }

    public float getAspectRatio(){
        if (curIndex < list.size() / 2){
            int width  = list.get(curIndex * 2);
            int height = list.get(curIndex * 2 + 1);
            if (height > 0){
                return (float)width / height;
            }
        }

        return 0f;
    }

    public int calHeight(int w){
        int width = 0;
        int height = 0;
        if (curIndex < list.size() / 2){
            width  = list.get(curIndex * 2);
            height = list.get(curIndex * 2 + 1);
        }
        if (width > 0){
            return w * height / width;
        }
        return 0;
    }

    public int calWidth(int h){
        int width = 0;
        int height = 0;
        if (curIndex < list.size() / 2){
            width  = list.get(curIndex * 2);
            height = list.get(curIndex * 2 + 1);
        }
        if (height > 0){
            return h * width / height;
        }
        return 0;
    }

    public int getResolutionWidth(int height){
        for (int i = 1; i < list.size(); i += 2){
            if (list.get(i) == height){
                return list.get(i - 1);
            }
        }
        return 0;
    }

    public int getResolutionHeight(int width){
        for (int i = 0; i < list.size() - 1; i += 2){
            if (list.get(i) == width){
                return list.get(i + 1);
            }
        }
        return 0;
    }

    public int getResolutionWidth(){
        if (curIndex < list.size() / 2 ){
            return list.get(curIndex * 2);
        }
        return 0;
    }

    public int getResolutionHeight(){
        if (curIndex < list.size() / 2){
            return list.get(curIndex * 2 + 1);
        }
        return 0;
    }
}
